package zzcltw.com.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



/**
 * 字符串工具类
 */
public final class StringUtil {
	/** Private Constructor **/
	private StringUtil() {
	}
	
	/**
	 * 判断字符串是否非null && 非空字符 
	 * 
	 * @param param
	 * @return
	 */
	public static boolean isNotEmpty(String param) {
		return param != null && !"".equals(param.trim());
	}

	/**
	 * 判断字符串是否为null || 空字符串
	 * 
	 * @param param
	 * @return
	 */
	public static boolean isEmpty(String param) {
		return param == null || "".equals(param.trim());
	}
	/**
	 * 判断是否为数字
	 * @param str
	 * @return True为数字
	 */
	public static boolean isNum(String str) {
		String regex = "^(-?\\d+)(\\.\\d+)?$";
		return matchRegex(str, regex);
	}
				
	private static boolean matchRegex(String value, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(value);
		return matcher.matches();
	}
	
	public static String trim2Empty(String str) {
		return isEmpty(str) ? "" : str.trim();
	}
	public static String replace(String old){
		return old.indexOf("html")>-1?old.replace("../", "/module/"):old.replace("../", "/front/");
	}
	public static String replaceChar(String param,String specialChar) {
		String newStr = param;//替换过后的字符；
		if(!StringUtil.isEmpty(param)){
			String replaceStr = param.replace(specialChar, "','");
			newStr = new StringBuilder().append("'").append(replaceStr).append("'").toString();
		}
		return newStr;
	}
	public static String getSafeFileName(String fileName){
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String dateStr = df.format(new Date());
		String tail = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
		dateStr = dateStr.concat(tail);
		if(StringUtil.isNotEmpty(fileName)){
			String fileExt = fileName.substring(fileName.lastIndexOf("."));
			dateStr = dateStr.concat(fileExt);
		}
		return dateStr;
	}
	/**
	 * 
	 * @Title replaceInputParamChar
	 * @Description: 替换前台传入参数中的特殊字符 ; < >  ( ) 
	 * @param: @param str
	 * @param: @return
	 * @return String
	 * @throws
	 * @author zzx
	 * @Date 2018年1月12日   下午5:03:51
	 */
	public static String replaceInputParamChar(String str){
		String newStr = str;
		if(StringUtil.isNotEmpty(newStr)){
			newStr = str.replace(";", "；");
			newStr = newStr.replace("<", "《");
			newStr = newStr.replace(">", "》");
			newStr = newStr.replace("(", "（");
			newStr = newStr.replace(")", "）");
		}
		return newStr;
	}
}
